import { StatisticCard } from '@ant-design/pro-card';
import { useState, useEffect } from 'react';

const { Statistic } = StatisticCard;

const ControlPathStatistic = (props) => {
  return (
    <StatisticCard
      collapsible
      title="控制权指数"
      statistic={{
        value: props.controlPower ? props.controlPower * 100 : '--',
        suffix: '%',
      }}
    />
  );
};

const ControlledNetworkStatistic = (props) => {
  const [info, setInfo] = useState({
    enterpriseWithBCP: {
      eid: '',
      name: '',
      controlPower: '',
    },
    personWithBCP: {
      eid: '',
      name: '',
      controlPower: '',
    },
    nodesCount: 0,
  });
  useEffect(() => {
    if (props.controlledNetwork.enterprise && props.controlledNetwork.controlledNetwork) {
      const nodes = props.controlledNetwork.controlledNetwork?.nodes;
      const nodesCount = nodes ? nodes.length : 0;
      const enterpriseWithBCP = {
        eid: '',
        name: '',
        controlPower: 0,
      };
      const personWithBCP = {
        eid: '',
        name: '',
        controlPower: 0,
      };
      nodes.forEach((node) => {
        if (
          node.id !== props.controlledNetwork.enterprise &&
          node.category === 'P' &&
          node.controlPower > personWithBCP.controlPower
        ) {
          personWithBCP.eid = node.id;
          personWithBCP.name = node.name;
          personWithBCP.controlPower = node.controlPower;
        }
        if (
          node.id !== props.controlledNetwork.enterprise &&
          node.category === 'E' &&
          node.controlPower > enterpriseWithBCP.controlPower
        ) {
          enterpriseWithBCP.eid = node.id;
          enterpriseWithBCP.name = node.name;
          enterpriseWithBCP.controlPower = node.controlPower;
        }
      });
      setInfo({
        enterpriseWithBCP,
        personWithBCP,
        nodesCount,
      });
    } else {
      setInfo({
        enterpriseWithBCP: {
          eid: '',
          name: '',
          controlPower: '',
        },
        personWithBCP: {
          eid: '',
          name: '',
          controlPower: '',
        },
        nodesCount: 0,
      });
    }
  }, [props.controlledNetwork]);
  return (
    <StatisticCard
      collapsible
      title="具有最大控制权公司"
      statistic={{
        value: info.enterpriseWithBCP.eid ? info.enterpriseWithBCP.name : '--',
        suffix: '',
      }}
      footer={
        <>
          <Statistic
            value={info.enterpriseWithBCP.eid ? info.enterpriseWithBCP.name : '--'}
            title="具有最大控制权公司"
            layout="horizontal"
          />
          <Statistic
            value={info.personWithBCP.eid ? info.personWithBCP.name : '--'}
            title="具有最大控制权自然人"
            layout="horizontal"
          />
          <Statistic
            value={info.nodesCount ? info.nodesCount : '--'}
            title="所属控制权网络结点数"
            layout="horizontal"
          />
        </>
      }
    />
  );
};

const ControllingNetworkStatistic = (props) => {
  const [info, setInfo] = useState({
    nodesCount: 0,
  });
  useEffect(() => {
    if (props.controllingNetwork.enterprise && props.controllingNetwork.controllingNetwork) {
      const nodes = props.controllingNetwork.controllingNetwork?.nodes;
      const nodesCount = nodes ? nodes.length : 0;
      setInfo({
        nodesCount,
      });
    } else {
      setInfo({
        nodesCount: 0,
      });
    }
  }, [props.controllingNetwork]);
  return (
    <StatisticCard
      collapsible
      title="控制结点数"
      statistic={{
        value: info.nodesCount ? info.nodesCount - 1 : '--',
        suffix: '个',
      }}
    />
  );
};

export { ControlPathStatistic, ControlledNetworkStatistic, ControllingNetworkStatistic };
